package com.yan.test.work.testpaging3.net;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class JsonContervUtil {

    public static String objectToStr(Object object){
        return new GsonBuilder().setDateFormat("yyyy-MM-dd") .disableHtmlEscaping().create().toJson(object);
    }

    public static<T> T jsonToObject(String json , Class<T> classOfT){
        return new Gson().fromJson(json , classOfT);
    }

    public static<T> T jsonToObject(String json ,Type typeOfT){
        return new Gson().fromJson(json , typeOfT);
    }

    public static<T> List<T> jsonToList(String json ,Class<T> classT){
        return new Gson().fromJson(json , new ParameterizedTypeImpl(classT));
    }


}

class ParameterizedTypeImpl implements ParameterizedType {
    Class clazz;

    public ParameterizedTypeImpl(Class clz) {
        clazz = clz;
    }

    @Override
    public Type[] getActualTypeArguments() {
        return new Type[]{clazz};
    }

    @Override
    public Type getRawType() {
        return List.class;
    }

    @Override
    public Type getOwnerType() {
        return null;
    }
}
